package view;

import controller.MyController;
import entity.Score;
import lombok.SneakyThrows;
import service.UserOpt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

/**
 * 用户首页
 */
public class Home extends JFrame implements ActionListener {
    JButton btn1,btn2,btn3,btn4;    // btn1 开始测评，btn2 历史成绩
    JLabel label,label2;
    JPanel panel1,panel2,panel3;
    ImageIcon icon1,icon2,icon3,icon4;
    UserLogin login;
    String name;
    public Home(UserLogin login,String username){
        super("小测评");
        ImageIcon icon = new ImageIcon("src\\main\\resources\\img\\icon7.png");
        this.setIconImage(icon.getImage());
        this.login = login;
        this.name = username;
        login.setVisible(false);
        Dimension preferredSize = new Dimension(100,100);
        icon1 = new ImageIcon("src\\main\\resources\\img\\icon1.png");
        icon1 = new ImageIcon(icon1.getImage().getScaledInstance(60,60,Image.SCALE_SMOOTH));
        icon2 = new ImageIcon("src\\main\\resources\\img\\icon2.png");
        icon2 = new ImageIcon(icon2.getImage().getScaledInstance(60,60,Image.SCALE_SMOOTH));
        icon3 = new ImageIcon("src\\main\\resources\\img\\icon3.png");
        icon3 = new ImageIcon(icon3.getImage().getScaledInstance(60,60,Image.SCALE_SMOOTH));
        icon4 = new ImageIcon("src\\main\\resources\\img\\icon4.png");
        icon4 = new ImageIcon(icon4.getImage().getScaledInstance(60,60,Image.SCALE_SMOOTH));
        btn1 = new JButton("开始测试",icon1);
        btn1.setPreferredSize(preferredSize);
        btn1.setVerticalTextPosition(JButton.BOTTOM);
        btn1.setHorizontalTextPosition(JButton.CENTER);
        btn2 = new JButton("历史成绩",icon2);
        btn2.setPreferredSize(preferredSize);
        btn2.setVerticalTextPosition(JButton.BOTTOM);
        btn2.setHorizontalTextPosition(JButton.CENTER);
        btn3 = new JButton("考试规则",icon3);
        btn3.setPreferredSize(preferredSize);
        btn3.setVerticalTextPosition(JButton.BOTTOM);
        btn3.setHorizontalTextPosition(JButton.CENTER);
        btn4 = new JButton("退出",icon4);
        btn4.setPreferredSize(preferredSize);
        btn4.setVerticalTextPosition(JButton.BOTTOM);
        btn4.setHorizontalTextPosition(JButton.CENTER);
        panel1 = new JPanel();
        panel1.add(btn1);
        panel1.add(btn2);
        panel1.add(btn3);
        panel1.add(btn4);
        panel2 = new JPanel();
        String s = "当前用户：\t"+username;
        label2 = new JLabel(s);
        panel3 = new JPanel();
        panel3.add(label2);
        label = new JLabel("在线测评系统");
        Font font = new Font("华文彩云",Font.BOLD,30);
        label.setFont(font);
        label.setForeground(Color.DARK_GRAY);
        panel2.add(label);
        this.setLayout(new BorderLayout());
        this.add(panel2,BorderLayout.NORTH);
        this.add(panel1,BorderLayout.CENTER);
        this.add(panel3,BorderLayout.SOUTH);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        this.setVisible(true);
        this.setResizable(false);
        this.setBounds(500,250,450,250);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @SneakyThrows
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn4){
            this.setVisible(false);
            this.login.setVisible(true);
        }
        if (e.getSource() == btn3){
            MyController.getExamRules();
        }
        if (e.getSource() == btn2){
            MyController.getUserScoreShow(this.name);
        }
        if (e.getSource() == btn1){
            MyController.getTestsMain(this,this.name);
        }
    }
}
